<?php
class Admin_MenuController extends Zend_Controller_Action{
	
	public function init(){
		Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
		$this->_model = new Default_Model_DbTable_Menu();
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		$this->view->fullName = $infoUser->ADMINNAME;
		$this->view->role = $infoUser->ROLE_ID;
	}
	public function indexAction(){
		$this->view->headTitle("Quản Lý Menu","PREPEND");
		$this->view->title = "Quản Lý Menu";
		$menus = $this->_model->getAllMenu();
		$this->view->allMenu = $menus;
		if($this->_request->isPost()){
			$upload = new Private_Upload();
			$name = $this->_getParam("Name");
			$parent = $this->_getParam("Parent");
			$desc = $this->_getParam("Desc");
			$stt = $this->_getParam("Stt");
			if($_FILES["Img"]["name"]== "" and $this->_getParam("Link")==""){
				$img = "";
			}elseif ($this->_getParam("Link")){
				$img = $this->_getParam("Link");
			}else{
				$img = "/homeviet/public/uploaded/images/".$_FILES["Img"]["name"];
				$upload->upload("menu");
			}
			$this->_model->addMenu($parent, $img, $name, $desc, $stt);
			$this->_redirect("admin/menu");
		}
	}
	
	public function editAction(){
		$this->view->headTitle("Chỉnh Sửa Menu","PREPEND");
		$this->view->title = "Chỉnh Sửa Menu";
		$this->view->allMenu = $this->_model->getAllMenu();
		$id = $this->_getParam("id");
		$this->view->menu = $this->_model->getMenuById($id);
		if($this->_request->isPost()){
			$upload = new Private_Upload();
			$name = $this->_getParam("Name");
			$parent = $this->_getParam("Parent");
			$desc = $this->_getParam("Desc");
			$stt = $this->_getParam("Stt");
			if($_FILES["Img"]["name"]== "" and $this->_getParam("Link")==""){
				$img = "";
			}elseif ($this->_getParam("Link")){
				$img = $this->_getParam("Link");
			}else{
				$img = "/homeviet/public/uploaded/images/menu/".$_FILES["Img"]["name"];
				$upload->upload("menu");
			}
			$this->_model->updateMenu($id,$parent, $img, $name, $desc, $stt);
			$this->_redirect("admin/menu");
		}
	}
	
	public function delAction(){
		$this->_model->removeMenu($this->_getParam("id"));
		$this->_redirect("admin/menu");
	}
	
	public function activeAction(){
		$id = $this->_getParam("id");
		$this->_model->activeMenu($id);
		$this->_redirect("admin/menu");
	}
}